package com.formina.engine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;


import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
import android.view.MotionEvent;


public class GameRenderer  implements Renderer {
	
	protected long loopStart = 0;
	protected long loopEnd = 0;
	protected long loopRunTime = 0 ;
	protected SoundPool soundPool;
	protected HashMap<Integer, Integer> soundPoolMap;
	float Width,Height;

	
	

	//FPS
    public double fps;
	//END FPS
    protected int programHandle;
    protected int vertexShaderHandle;
    protected int fragmentShaderHandle;
    
    protected String GetVertexShaderSolid(){
    	return
    	  "uniform mat4 u_MVPMatrix;      \n"     // A constant representing the combined model/view/projection matrix.
        + "attribute vec4 a_Color;        \n"     // Per-vertex color information we will pass in.
        + "attribute vec4 a_Position;     \n"     // Per-vertex position information we will pass in.
        + "varying vec4 v_Color;          \n"     // This will be passed into the fragment shader.       
        + "void main()                    \n"     // The entry point for our vertex shader.
        + "{                              \n"
        + "   v_Color = a_Color;          \n"     // Pass the color through to the fragment shader.                                         // It will be interpolated across the triangle.
        + "   gl_Position = u_MVPMatrix   \n"     // gl_Position is a special variable used to store the final position.
        + "               * a_Position;   \n"     // Multiply the vertex by the matrix to get the final point in
        + "}                              \n";    // normalized screen coordinates.
    }
    
   
    protected String GetFragmentShader ()
    {
    	return
    	"precision mediump float;       \n"     // Set the default precision to medium. We don't need as high of a
    	+ "varying vec4 v_Color;          \n"     // This is the color from the vertex shader interpolated across the                                            // triangle per fragment.
    	+ "void main()                    \n"     // The entry point for our fragment shader.
    	+ "{                              \n"
    	//+ " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n"
    	+ " gl_FragColor = v_Color; \n"
    	+ "}                              \n";
    }
    
    
	public GameRenderer(){
		
	}
		
	public void HandleTouch(MotionEvent ev){
		
		
	}
	
	
	
	public void playSound(int sound) {
	    /* Updated: The next 4 lines calculate the current volume in a scale of 0.0 to 1.0 */
	    AudioManager mgr = (AudioManager)Engine.CONTEXT.getSystemService(Context.AUDIO_SERVICE);
	    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
	    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    
	    float volume = streamVolumeCurrent / streamVolumeMax;
	    
	    /* Play the sound with the correct volume */
	    //soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);     
	}
	
	public void onDrawFrame(GL10 gl) {
		
	}
	
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		
	     
	}
	
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		
	}
	
	public int compileShader(final int shaderType, final String shaderSource) 
	{
		int shaderHandle = GLES20.glCreateShader(shaderType);

		if (shaderHandle != 0) 
		{
			// Pass in the shader source.
			GLES20.glShaderSource(shaderHandle, shaderSource);

			// Compile the shader.
			GLES20.glCompileShader(shaderHandle);

			// Get the compilation status.
			final int[] compileStatus = new int[1];
			GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

			// If the compilation failed, delete the shader.
			if (compileStatus[0] == 0) 
			{
				Log.e("GameRenderer", "Error compiling shader: " + GLES20.glGetShaderInfoLog(shaderHandle));
				GLES20.glDeleteShader(shaderHandle);
				shaderHandle = 0;
			}
		}

		if (shaderHandle == 0)
		{			
			throw new RuntimeException("Error creating shader.");
		}
		
		return shaderHandle;
	}	
	
	public int CreateAndLinkProgram(final int vertexShaderHandle, final int fragmentShaderHandle, final String[] attributes) 
	{
		int programHandle = GLES20.glCreateProgram();
		
		if (programHandle != 0) 
		{
			// Bind the vertex shader to the program.
			GLES20.glAttachShader(programHandle, vertexShaderHandle);			

			// Bind the fragment shader to the program.
			GLES20.glAttachShader(programHandle, fragmentShaderHandle);
			
			// Bind attributes
			if (attributes != null)
			{
				final int size = attributes.length;
				for (int i = 0; i < size; i++)
				{
					GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
				}						
			}
			
			// Link the two shaders together into a program.
			GLES20.glLinkProgram(programHandle);

			// Get the link status.
			final int[] linkStatus = new int[1];
			GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

			// If the link failed, delete the program.
			if (linkStatus[0] == 0) 
			{				
				Log.e("GameRenderer", "Error compiling program: " + GLES20.glGetProgramInfoLog(programHandle));
				GLES20.glDeleteProgram(programHandle);
				programHandle = 0;
			}
		}
		
		if (programHandle == 0)
		{
			throw new RuntimeException("Error creating program.");
		}
		
		return programHandle;
	}
	 
}
	

